package cn.iocoder.yudao.module.famous.doctor.service.attention;

import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.attention.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.attention.AttentionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.attention.AttentionConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.attention.AttentionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 我的关注 Service 实现类
 *
 * @author 启航
 */
@Service
@Validated
public class AttentionServiceImpl implements AttentionService {

    @Resource
    private AttentionMapper attentionMapper;

    @Override
    public Long createAttention(AttentionCreateReqVO createReqVO) {
        // 插入
        AttentionDO attention = AttentionConvert.INSTANCE.convert(createReqVO);
        attentionMapper.insert(attention);
        // 返回
        return attention.getId();
    }

    @Override
    public void updateAttention(AttentionUpdateReqVO updateReqVO) {
        // 校验存在
        validateAttentionExists(updateReqVO.getId());
        // 更新
        AttentionDO updateObj = AttentionConvert.INSTANCE.convert(updateReqVO);
        attentionMapper.updateById(updateObj);
    }

    @Override
    public void deleteAttention(Long id) {
        // 校验存在
        validateAttentionExists(id);
        // 删除
        attentionMapper.deleteById(id);
    }

    private void validateAttentionExists(Long id) {
        if (attentionMapper.selectById(id) == null) {
            throw exception(ATTENTION_NOT_EXISTS);
        }
    }

    @Override
    public AttentionDO getAttention(Long id) {
        return attentionMapper.selectById(id);
    }

    @Override
    public List<AttentionDO> getAttentionList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return attentionMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<AttentionDO> getAttentionPage(AttentionPageReqVO pageReqVO) {
        return attentionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AttentionDO> getAttentionList(AttentionExportReqVO exportReqVO) {
        return attentionMapper.selectList(exportReqVO);
    }

    @Override
    public IPage<AttentionPageRespVO> getAttentionMessagePage(AttentionPageReqVO pageVO) {
        IPage<AttentionPageRespVO> resultlist= attentionMapper.getAttentionMessagePage(MyBatisUtils.buildPage(pageVO), pageVO,getLoginUserId());
        return resultlist;
    }

}
